package com.example.flo;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    int SCREEN_HEIGHT,SCREEN_WIDTH;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button getpower = findViewById(R.id.bt_getpower);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button getpower2 = findViewById(R.id.bt_getpower2);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button selfopen = findViewById(R.id.bt_selfopen);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button open = findViewById(R.id.bt_open);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"})Button mv = findViewById(R.id.bt_mv);

        getpower.setOnClickListener(this);
        getpower2.setOnClickListener(this);
        open.setOnClickListener(this);
        mv.setOnClickListener(this);
        selfopen.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_getpower)
        {
            // 申请悬浮窗权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) // 判断系统版本
            {
                if (!Settings.canDrawOverlays(this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
                else {
                    Toast.makeText(this, "已获取权限", Toast.LENGTH_SHORT).show();
                }
            }
        } else if (v.getId() == R.id.bt_open) {
            Log.d("float","打开");
            floatview.getInstance(this).createFloatView();
        } else if (v.getId() == R.id.bt_mv) {
            Log.d("float","关闭");
            floatview.getInstance(this).removeFloatView();
        } else if (v.getId() == R.id.bt_getpower2) {    // 打开无障碍权限
            if (MyAccessibilityService.getInstance() != null) {
                Toast.makeText(this, "已获取权限", Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
        else if (v.getId() == R.id.bt_selfopen) {   // 打开自启管理
            try {
                // 小米
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                startActivity(intent);
            } catch (Exception e) {
                // 如果上述方法失败，则提示用户手动前往设置
                Toast.makeText(this, "无法自动跳转，请手动前往设置->权限管理->自启动管理", Toast.LENGTH_LONG).show();
            }
        }
    }
}

